package derbyviewer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import ooboo.Assets;

public class DerbyViewer extends JFrame
{
	private static final long serialVersionUID = 1l;

	private DefaultListModel listModel;
	private JList list;
	
	private DefaultTableModel tableModel;
	private JTable table;
	
	private Connection con;
	
	public DerbyViewer()
	{
		Container mainWindow = getContentPane();
		mainWindow.setLayout(new BorderLayout());
		{
			listModel = new DefaultListModel();
			list = new JList(listModel);
			list.getSelectionModel().addListSelectionListener(
					new ListSelectionListener()
					{
						public void valueChanged(ListSelectionEvent e)
						{
							if(list.getSelectedIndex() >= 0)
								selectTable(list.getSelectedValue().toString());
						}
					});
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			mainWindow.add(new JScrollPane(list),BorderLayout.WEST);
			
			tableModel = new DefaultTableModel();
			table = new JTable(tableModel);
			mainWindow.add(new JScrollPane(table),BorderLayout.CENTER);
		}
		
		connect();
		refresh();
		
		pack();
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("ooboo Database Viewer");
		setVisible(true);
	}
	
	private void connect()
	{
		try
		{
			System.setProperty("derby.system.home",Assets.getAppDataFolder().getPath());
			
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			con = DriverManager.getConnection("jdbc:derby:storage");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void refresh()
	{
		try
		{
			listModel.clear();
			
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(
					"SELECT S.SCHEMANAME || '.' || T.TABLENAME " +
					"FROM SYS.SYSTABLES T, SYS.SYSSCHEMAS S " +
					"WHERE T.SCHEMAID = S.SCHEMAID AND T.TABLETYPE = 'T'");
			while(rs.next())
				listModel.addElement(rs.getString(1));
			
			if(list.getSelectedIndex() >= 0)
				selectTable(list.getSelectedValue().toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void selectTable(String tableName)
	{
		try
		{
			while(tableModel.getRowCount() > 0)
				tableModel.removeRow(0);
			
			Vector<Vector<Object>> dataVector = new Vector<Vector<Object>>();
			Vector<Object> columnIdentifiers = new Vector<Object>();
			
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
			ResultSetMetaData rsmd = rs.getMetaData();
			
			for(int i = 0; i < rsmd.getColumnCount(); i++)
				columnIdentifiers.add(rsmd.getColumnName(i + 1));
			
			while(rs.next() && dataVector.size() < 1000)
			{
				Vector<Object> row = new Vector<Object>();
				
				for(int i = 0; i < rsmd.getColumnCount(); i++)
					row.add(rs.getObject(i + 1));
				
				dataVector.add(row);
			}
			
			tableModel.setDataVector(dataVector,columnIdentifiers);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		new DerbyViewer();
	}
}
